package com.xufx.thread.liaoxuefeng.thread9_reentranrLockAndSemaphore;

import javafx.concurrent.Task;

import java.util.concurrent.Semaphore;

public class SemaphoreRunner {


    public static void main(String[] args) {

        Semaphore semaphore = new Semaphore(2);
        for (int i = 0; i < 10; i++){
            new Thread(new Task(semaphore, "yangguo" + i)).start();
        }

    }
    static class Task extends Thread{
        Semaphore semaphore;
        public Task(Semaphore semaphore, String tname){
            super(tname);
            this.semaphore = semaphore;
        }

        public void run(){
            try{
                semaphore.acquire();
                System.out.println(Thread.currentThread().getName() + ":acquire() at time:" + System.currentTimeMillis()) ;
                Thread.sleep(5000);
                semaphore.release();
            }catch (InterruptedException e){
                e.printStackTrace();
            }
        }
    }

}
